#include "queue_linklist.h"

int QnodeList_init(QnodeList_t ** queue){
    if(*queue!=NULL){
        return -1;
    }
    (*queue) = (QnodeList_t *)malloc(sizeof(QnodeList_t));
    if((*queue) == NULL){
        LOG("queue create failed\n");
        return -1;
    }
    //创建一个头节点
    Qnode_t * head = (Qnode_t*)malloc(sizeof(Qnode_t));
    if(head == NULL){
        LOG("node create failed\n");
        return -1;
    }
    (*queue)->font = head;
    (*queue)->rear = head;
    head->next = NULL;
    LOG("queue create success\n");
    return 0;
}

static int QnodeList_isNULL(QnodeList_t * queue){
    if(queue->font == queue->rear){
        return 0;
    }
    return -1;
}

int QnodeList_enqueue(QnodeList_t * queue,int data){
    if(queue==NULL){
        return -1;
    }
    Qnode_t * node = (Qnode_t*)malloc(sizeof(Qnode_t));
    if(node == NULL){
        LOG("node create failed\n");
        return -1;
    }
    node->data = data;
    node->next = NULL;
    queue->rear->next = node;
    queue->rear = node;
    return 0;
}

int QnodeList_dequeue(QnodeList_t * queue,Qnode_t * node){
    if(queue==NULL){
        return -1;
    }
    Qnode_t * font = queue->font->next;
    memcpy(node,font,sizeof(Qnode_t));
    queue->font->next = font->next;
    free(font);
    return 0;
}

int QnodeList_select(QnodeList_t * queue){
    if(queue == NULL){
        return -1;
    }
    Qnode_t * p = queue->font->next;
    while (p!=NULL)
    {
        printf("%d->",p->data);
        p = p->next;
    }
    printf("NULL\n");
    return 0;
}

int QnodeList_destory(QnodeList_t * queue){
    if(queue==NULL){
        return -1;
    }
    Qnode_t * p = queue->font->next;
    while (p!=NULL)
    {
        Qnode_t *q = p->next;
        free(p);
        p = q;
    }
    free(queue->font);
    free(queue);
    queue = NULL;
    return 0;
}